package com.example.yiqiuser.service.impl;




import com.example.yiqiuser.dao.SbsnOrderDao;
import com.example.yiqiuser.dao.TSbsnActDao;
import com.example.yiqiuser.dao.TSbsnCode;
import com.example.yiqiuser.entity.SbsnOrder;
import com.example.yiqiuser.entity.Scenic;
import com.example.yiqiuser.entity.TSbsnAct;
import com.example.yiqiuser.service.SbsnOrderService;
import com.example.yiqiuser.service.ScenicService;
import com.example.yiqiuser.util.ConstantUtil;
import com.example.yiqiuser.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ fileName:SbsnOrderServiceImpl
 * @ description:
 * @ author:xjd
 * @ createTime:2023/4/1 8:32
 * @ version:1.0.0
 */
@Service
public class SbsnOrderServiceImpl implements SbsnOrderService {

    //依赖注入
    @Resource
    private SbsnOrderDao sbsnOrderDao;

    @Resource
    private TSbsnActDao tSbsnActDao;

    @Resource
    private TSbsnCode tSbsnCode;

    @Resource
    private ScenicService scenicService;

    /**
     * 根据
     * @param page
     * @return
     */
    @Override
    public PageInfo queryByPage(Page<SbsnOrder> page) {

        //设置页码和每页显示数量
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(sbsnOrderDao.queryByParam(page.getData()));
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<SbsnOrder> queryAll() {

        return sbsnOrderDao.queryAll();
    }

    /**
     * 根据Id查询
     * @param orderid
     * @return
     */
    @Override
    public SbsnOrder queryById(int orderid) {
        List<SbsnOrder> sbsnOrderList = sbsnOrderDao.queryById(orderid);
        //判断sbsnOrderList是否为空
        if (!CollectionUtils.isEmpty(sbsnOrderList)){
            return sbsnOrderList.get(0);
        }
        return null;
    }


    /**
     * 设置订单的初始化信息
     * @param sbsnOrder 前台传过来的订单
     * @return
     */
    @Override
    public SbsnOrder add(SbsnOrder sbsnOrder) {

     //   System.out.println(sbsnOrder+"!!!!!!!!!!");
        //设置参数 代付款
        sbsnOrder.setCodeid(ConstantUtil.OrderStatus.PENDING_PAYMENT);
        sbsnOrder.setState(ConstantUtil.OrderStatus.PENDING_PAYMENT);
        sbsnOrder.setOrdertime(new Date());
        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(sbsnOrder.getQprsj())+"....................");
        try {
            sbsnOrder.setQprsj(simpleDateFormat.parse(simpleDateFormat.format(sbsnOrder.getQprsj())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(sbsnOrder.getQprsj()+".............1111111111.....................");*/
        //返回添加的主键
        int add = sbsnOrderDao.add(sbsnOrder);
        //添加状态
        TSbsnAct tSbsnAct = new TSbsnAct();
        tSbsnAct.setOrderid(sbsnOrder.getOrderid());
        tSbsnAct.setCodeid(sbsnOrder.getCodeid());
        tSbsnAct.setActcode(sbsnOrder.getCodeid());
        //查询动作名称
        tSbsnAct.setActname(tSbsnCode.queryName(sbsnOrder.getCodeid()));
        tSbsnAct.setBz("下单");
        tSbsnAct.setCztime(sbsnOrder.getOrdertime());
        tSbsnAct.setCzrid(sbsnOrder.getUserid());
        //调用添加
        tSbsnActDao.add(tSbsnAct);

        System.out.println(sbsnOrder.getScenicid()+"=================222222222222222222==============");
        //景区添加票数
        Scenic scenic = scenicService.queryById(sbsnOrder.getScenicid());
        Integer onums = scenic.getOnums();
        System.out.println("------------------"+onums+"------------------");
        scenic.setOnums(onums+1);
        //修改订票信息
        scenicService.update(scenic);

        return sbsnOrder;
    }

    /**
     * 更改订单状态
     * @param sbsnOrder 更改订单状态
     * @return
     */
    @Override
    public int update(SbsnOrder sbsnOrder) {
        //设置参数 已付款
        sbsnOrder.setCodeid(ConstantUtil.OrderStatus.PAID);
        sbsnOrder.setState(ConstantUtil.OrderStatus.PAID);
        int update = sbsnOrderDao.update(sbsnOrder);
        //设置订单状态
        TSbsnAct tSbsnAct = tSbsnActDao.queryById(sbsnOrder.getOrderid());
        tSbsnAct.setCodeid(sbsnOrder.getCodeid());
        tSbsnAct.setActcode(sbsnOrder.getCodeid());
        //查询动作名称
        tSbsnAct.setActname(tSbsnCode.queryName(tSbsnAct.getCodeid()));
        tSbsnAct.setBz("付款成功");
        tSbsnActDao.update(tSbsnAct);
        //更新景区门票
        Scenic scenic = (Scenic) scenicService.queryById(sbsnOrder.getScenicid());
        Integer snums = scenic.getSnums();
        scenic.setSnums(snums+1);
        Integer onums = scenic.getOnums();
        scenic.setOnums(onums-1);
        scenicService.update(scenic);

        return update;
    }

    /**
     * 根据Id删除
     * @param orderid
     * @return
     */
    @Override
    public int deleteById(int orderid) {
        return sbsnOrderDao.deleteById(orderid);
    }
}
